package info.openlvb.openface.client.handler;

import java.util.Date;
import info.openlvb.openface.client.Openface;
import info.openlvb.openface.client.images.OpenFaceImages;
import info.openlvb.openface.client.service.GraphService;
import info.openlvb.openface.client.service.GraphServiceAsync;
import com.google.gwt.core.client.*;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;
import com.google.gwt.user.datepicker.client.DatePicker;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.layout.HorizontalLayout;


public class Graph_Ok_Handler implements ClickHandler, KeyUpHandler{

	private Openface openFace;
	private ClickEvent event;
	String[] arg;

	private final GraphServiceAsync GraphService = GWT.create(GraphService.class);

	OpenFaceImages images = GWT.create(OpenFaceImages.class);

	public Graph_Ok_Handler(){ //RootPanel rootPanel, String login, String password){
	}
	@Override
	public void onClick(ClickEvent event) {
		this.event = event;
		final Window dialogBox = createDialogBox(event);
		dialogBox.show();
	}
	@Override
	public void onKeyUp(KeyUpEvent event) {
	}
	/**
	 * Create the dialog box for this example.
	 * 
	 * @return the new dialog box
	 */
	private Window createDialogBox(ClickEvent event) {
		// Create a dialog box and set the caption text

		final Window window = new Window();
		window.setTitle("Personnalisation des graphes");
		window.setClosable(true);
		window.setPlain(true);
		window.setLayout(new HorizontalLayout(15));
		final Image img = new Image();
		img.setUrl(((Image) event.getSource()).getUrl());
		window.add(img);
		final FlexTable flexTable = new FlexTable();
		window.setCloseAction(Window.HIDE);
		flexTable.ensureDebugId("cwFlexTable");
		flexTable.setStyleName("flexTable");
		//flexTable.setBorderWidth(1);
		FlexCellFormatter cellFormatter = flexTable.getFlexCellFormatter();
		// Add some text
		flexTable.setHTML(0, 0, "Dates");
		cellFormatter.setStyleName(0, 0, "cellFlexTable");
		cellFormatter.setColSpan(0, 0, 4);
		flexTable.setHTML(1, 0,"Start day :");
		cellFormatter.setStyleName(1, 0, "cellFlexTable");
		cellFormatter.setColSpan(1, 0, 2);
		flexTable.setHTML(1, 1, "End day :");
		cellFormatter.setStyleName(1, 1, "cellFlexTable");
		cellFormatter.setColSpan(1, 1, 2);

		final TextBox dateBox1 = new TextBox();

		dateBox1.setText(DateTimeFormat.getShortDateFormat().format(new Date()));
		final TextBox dateBox2 = new TextBox();
		dateBox2.setText(DateTimeFormat.getShortDateFormat().format(new Date()));
		//dateBox1.setFormat(new DateBox.DefaultFormat(dateFormat));
		dateBox1.setWidth("75px");
		//dateBox2.setFormat(new DateBox.DefaultFormat(dateFormat));
		dateBox2.setWidth("75px");
		PushButton buttonDate1 = new PushButton(images.calendar().createImage() ,
				new ClickHandler() {
			public void onClick(ClickEvent event) {
				final Window window = new Window();
				DatePicker Date1 = new DatePicker();
				window.add(Date1);
				Button closeButton = new Button("CLOSE",
						new ClickHandler() {
					public void onClick(ClickEvent event) {
						window.hide();
					}
				});
				Date1.addValueChangeHandler(new ValueChangeHandler() {
					public void onValueChange(ValueChangeEvent event) {
						dateBox1.setText(DateTimeFormat.getShortDateFormat().format((Date) event.getValue()));
						window.hide();
					}
				});
				window.show();
			}
		});
		PushButton buttonDate2 = new PushButton(images.calendar().createImage(),
				new ClickHandler() {
			public void onClick(ClickEvent event) {
				final Window window = new Window();
				DatePicker Date2 = new DatePicker();
				window.add(Date2);
				Button closeButton = new Button("CLOSE",
						new ClickHandler() {
					public void onClick(ClickEvent event) {
						window.hide();
					}
				});
				Date2.addValueChangeHandler(new ValueChangeHandler() {
					public void onValueChange(ValueChangeEvent event) {
						dateBox2.setText(DateTimeFormat.getShortDateFormat().format((Date) event.getValue()));
						window.hide();
					}
				});
				window.show();
			}
		});
		HorizontalPanel hp1 = new HorizontalPanel();
		HorizontalPanel hp2 = new HorizontalPanel();
		hp1.add(dateBox1);
		hp1.add(buttonDate1);
		hp2.add(dateBox2);
		hp2.add(buttonDate2);
		// Set the value in the text box when the user selects a date
		flexTable.setWidget(2, 0, hp1);
		cellFormatter.setColSpan(2, 0, 2);
		flexTable.setWidget(2, 1, hp2);
		cellFormatter.setColSpan(2, 1, 2);
		flexTable.setHTML(3, 0, "Times");
		cellFormatter.setStyleName(3, 0, "cellFlexTable");
		cellFormatter.setColSpan(3, 0, 4);
		final TextBox text1 = new TextBox();
		final TextBox text2 = new TextBox();
		text1.setWidth("55");
		text2.setWidth("55");
		text1.setText(Integer.toString(new Date().getHours()-3)+":"+Integer.toString(new Date().getMinutes()));
		text2.setText(Integer.toString(new Date().getHours())+":"+Integer.toString(new Date().getMinutes()));
		flexTable.setHTML(4, 0, "Start time :");
		cellFormatter.setStyleName(4, 0, "cellFlexTable");
		cellFormatter.setColSpan(4, 0, 2);
		flexTable.setWidget(5, 0, text1);
		cellFormatter.setStyleName(5, 0, "cellFlexTable");
		cellFormatter.setColSpan(5, 0, 2);
		flexTable.setHTML(4, 1, "End time :");
		cellFormatter.setStyleName(4, 1, "cellFlexTable");
		cellFormatter.setColSpan(4, 1, 2);
		flexTable.setWidget(5, 1, text2);
		cellFormatter.setStyleName(5, 1, "cellFlexTable");
		cellFormatter.setColSpan(5, 1, 2);
		Button closeButton = new Button("CLOSE",
				new ClickHandler() {
			public void onClick(ClickEvent event) {
				window.hide();
			}
		});
		closeButton.setStyleName("graphButton");
		Button applyButton = new Button("APPLY",
				new ClickHandler() {
			public void onClick(ClickEvent event) {
				 String[] arg={"cpu",dateBox1.getText(),dateBox2.getText(),text1.getText(),text2.getText()};
				GraphService.img(arg,new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(Boolean result) {
						// TODO Auto-generated method stub
					img.setUrl("images/cpu.png?"+new Date().getTime());	
					}

				});
			}
		});
				applyButton.setStyleName("graphButton");
				VerticalPanel vp = new VerticalPanel();
				vp.add(flexTable);
				HorizontalPanel hp3 = new HorizontalPanel();
				hp3.add(applyButton);
				hp3.add(closeButton);
				vp.add(hp3);
				window.add(vp);
				return window;
}
}
